package com.xiaosheng.ljswl.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xiaosheng.ljswl.Config.SerialApplication;
import com.xiaosheng.ljswl.R;
import com.xiaosheng.ljswl.model.Node.*;

import node.utils.Sensor;
import serial.utils.SerialZbThread;

public class HongWaiCeJuFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private TextView MacAdd;
    private TextView result;
    private HongWaiCeJu hongWaiCeJu = new HongWaiCeJu();

    public HongWaiCeJuFragment() {
    }

    public static WenShiDuFragment newInstance(String param1, String param2) {
        WenShiDuFragment fragment = new WenShiDuFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_hong_wai_ce_ju, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        MacAdd = (TextView) view.findViewById(R.id.MacAdd);
        result = (TextView) view.findViewById(R.id.result);
        SerialZbThread serialZbThread = ((SerialApplication) getActivity().getApplication()).getSerialZbThread();
        serialZbThread.setiZgibeeRec((buffer, len) -> {
            boolean flag = Sensor.getHongWaiCeJu(this.hongWaiCeJu, buffer, len);
            if (!flag)
                serialZbThread.resetComDataHandler();
            else
                getActivity().runOnUiThread(() -> {
                    MacAdd.setText(this.hongWaiCeJu.mac);
                    result.setText(this.hongWaiCeJu.juli+"CM");
                });
        });
    }
}